﻿using System;
using System.Collections;
using System.Data;
using System.Data.SqlClient;
using Microsoft.Practices.EnterpriseLibrary.Data;
using Microsoft.Practices.EnterpriseLibrary.Data.Sql;
using Csla;

namespace Model
{
    [Serializable()]
    public class DuongPhoBOCollection : Csla.BusinessListBase<DuongPhoBOCollection, DuongPhoBO>
    {
        public GiaDatBOCollection giaDatBOCollection = null;
        private Hashtable hstTenDuongPhoStartIndex = new Hashtable(); //TenDuongPhoID; startIndex

        #region BindingList Overrides
        protected override object AddNewCore()
        {
            DuongPhoBO _DuongPhoBO = DuongPhoBO.NewChildDuongPhoBO();
            OnAddingNew(new System.ComponentModel.AddingNewEventArgs(_DuongPhoBO));
            Add(_DuongPhoBO);
            return _DuongPhoBO;
        }
        #endregion BindingList Overrides
        //==========================================================================

        #region Factory Methods
        private DuongPhoBOCollection()
        {
        }

        public static DuongPhoBOCollection NewDuongPhoBOCollection()
        {
            return new DuongPhoBOCollection();
        }

        public static DuongPhoBO NewDuongPhoBO()
        {
            return DuongPhoBO.NewChildDuongPhoBO();
        }

        public static DuongPhoBOCollection GetDuongPhoBOCollection()
        {
            SPParam spParam = new SPParam("DuongPhoID", "ASC", "WHERE DELE=0");
            return DataPortal.Fetch<DuongPhoBOCollection>(spParam);
        }

        public static DuongPhoBOCollection GetDuongPhoBOCollection(bool getDeleted)
        {
            SPParam spParam = new SPParam("DuongPhoID", "ASC", "");
            return DataPortal.Fetch<DuongPhoBOCollection>(spParam);
        }

        public static DuongPhoBOCollection GetDuongPhoBOCollection(string where)
        {
            SPParam spParam = new SPParam("DuongPhoID", "ASC", "WHERE DELE=0" + (where != "" ? " AND " : "") + where);
            return DataPortal.Fetch<DuongPhoBOCollection>(spParam);
        }

        public static DuongPhoBOCollection GetDuongPhoBOCollection(string where, bool getDeleted)
        {
            SPParam spParam = new SPParam("DuongPhoID", "ASC", (where != "" ? "WHERE " : "") + where);
            return DataPortal.Fetch<DuongPhoBOCollection>(spParam);
        }

        public static DuongPhoBOCollection GetDuongPhoBOCollection(string where, string orderByColumn)
        {
            SPParam spParam = new SPParam((orderByColumn == "" ? "DuongPhoID" : orderByColumn), "ASC", "WHERE DELE=0" + (where != "" ? " AND " : "") + where);
            return DataPortal.Fetch<DuongPhoBOCollection>(spParam);
        }

        public static DuongPhoBOCollection GetDuongPhoBOCollection(string where, string orderByColumn, bool getDeleted)
        {
            SPParam spParam = new SPParam((orderByColumn == "" ? "DuongPhoID" : orderByColumn), "ASC", (where != "" ? "WHERE " : "") + where);
            return DataPortal.Fetch<DuongPhoBOCollection>(spParam);
        }

        public static DuongPhoBOCollection GetDuongPhoBOCollection(string where, string orderByColumn, string orderByExpression)
        {
            SPParam spParam = new SPParam((orderByColumn == "" ? "DuongPhoID" : orderByColumn), (orderByExpression == "" ? "ASC" : orderByExpression), "WHERE DELE=0" + (where != "" ? " AND " : "") + where);
            return DataPortal.Fetch<DuongPhoBOCollection>(spParam);
        }

        public static DuongPhoBOCollection GetDuongPhoBOCollection(string where, string orderByColumn, string orderByExpression, bool getDeleted)
        {
            SPParam spParam = new SPParam((orderByColumn == "" ? "DuongPhoID" : orderByColumn), (orderByExpression == "" ? "ASC" : orderByExpression), (where != "" ? "WHERE " : "") + where);
            return DataPortal.Fetch<DuongPhoBOCollection>(spParam);
        }
        #endregion Factory Methods
        //==========================================================================

        #region Criteria
        [Serializable()]
        private class Criteria
        {
            public Criteria()
            {
            }
        }

        [Serializable()]
        public class FilterCriteria
        {
            public FilterCriteria()
            {
            }
        }
        #endregion Criteria
        //==========================================================================

        #region Fetch
        [Transactional(TransactionalTypes.TransactionScope)]
        private void DataPortal_Fetch(SPParam spParam)
        {
            this.hstTenDuongPhoStartIndex.Clear();
            int TenDuongPhoID = -1;

            this.giaDatBOCollection = GiaDatBOCollection.GetGiaDatBOCollection("", "NhomGiaDatID,SoThuTu");
            GiaDatBO giaDatBO = null;

            RaiseListChangedEvents = false;
            SqlDatabase db = new SqlDatabase(Session.DBConnectionString);
            SqlCommand cmd = (SqlCommand)db.GetStoredProcCommand("DuongPhoBO_SelectBOCollection");
            db.AddInParameter(cmd, "@OrderColumn", SqlDbType.VarChar, (object)spParam.orderColumn);
            db.AddInParameter(cmd, "@OrderExpression", SqlDbType.VarChar, (object)spParam.orderExpression);
            db.AddInParameter(cmd, "@WhereExpression", SqlDbType.NVarChar, (object)spParam.whereExpression);
            using (SqlDataReader dr = (SqlDataReader)db.ExecuteReader(cmd))
            {
                while (dr.Read())
                {
                    DuongPhoBO objDuongPhoBO = DuongPhoBO.GetChildDuongPhoBO(dr);

                    if (objDuongPhoBO.CL_GiaDatID != null)
                    {
                        giaDatBO = this.giaDatBOCollection.GetGiaDatBO(objDuongPhoBO.CL_GiaDatID.Value);
                        if (giaDatBO != null)
                            if (giaDatBO.CL_GiaChuan != null)
                                objDuongPhoBO.CL_GiaChuan = giaDatBO.CL_GiaChuan;
                    }

                    if (objDuongPhoBO.CL_GiaChuan != null && objDuongPhoBO.CL_HeSo != null)
                        objDuongPhoBO.CL_Gia1m2 = objDuongPhoBO.CL_GiaChuan.Value * objDuongPhoBO.CL_HeSo.Value;

                    this.Add(objDuongPhoBO);

                    if (objDuongPhoBO.CL_TenDuongPhoID != null)
                        if (TenDuongPhoID != objDuongPhoBO.CL_TenDuongPhoID.Value)
                        {
                            TenDuongPhoID = objDuongPhoBO.CL_TenDuongPhoID.Value;
                            this.hstTenDuongPhoStartIndex.Add(TenDuongPhoID, this.Count - 1);
                        }
                }
            }
            RaiseListChangedEvents = true;
        }
        #endregion Fetch
        //==========================================================================

        #region Update
        [Transactional(TransactionalTypes.TransactionScope)]
        protected override void DataPortal_Update()
        {
            Update();
        }

        internal void Update()
        {
            RaiseListChangedEvents = false;
            DuongPhoBO child = null;
            int length = this.Count;
            int stt = 0;
            bool del = false;
            for (int i = 0; i < length; i++)
            {
                child = this[i];
                del = StaticUtil.GetBool(child.CL_DELE);

                if (child.IsNew && !del)
                    child.Insert();
                else
                    child.Update();

                if (del)
                {
                    this.RemoveAt(i);
                    i--;
                    length--;
                }
                else
                {
                    child.STT0 = ++stt;
                }
            }
            RaiseListChangedEvents = true;
        }
        #endregion Update
        //==========================================================================

        #region Business Method
        public DuongPhoBO GetDuongPhoBO(int _DuongPhoID)
        {
            DuongPhoBO returnObject = null;
            foreach (DuongPhoBO _DuongPhoBO in this)
            {
                if (_DuongPhoBO.CL_DuongPhoID == _DuongPhoID)
                {
                    returnObject = _DuongPhoBO;
                    break;
                }
            }
            return returnObject;
        }

        public void Remove(int DuongPhoID)
        {
            foreach (DuongPhoBO _DuongPhoBO in this)
            {
                if (_DuongPhoBO.CL_DuongPhoID == DuongPhoID)
                {
                    try
                    {
                        this.Remove(_DuongPhoBO);
                    }
                    catch { }
                    break;
                }
            }
        }

        public DuongPhoBOCollection FilterDuongPhoBOCollection(int? TenDuongPhoID)
        {
            DuongPhoBOCollection duongPhoBOCollection = DuongPhoBOCollection.NewDuongPhoBOCollection();

            if (TenDuongPhoID != null)
                if (this.hstTenDuongPhoStartIndex.ContainsKey(TenDuongPhoID.Value))
                {
                    int istart = (int)this.hstTenDuongPhoStartIndex[TenDuongPhoID.Value];
                    int length = this.Count;
                    for (int i = istart; i < length; i++)
                    {
                        if (this[i].CL_TenDuongPhoID != TenDuongPhoID)
                            break;

                        duongPhoBOCollection.Add(this[i].Clone());
                    }
                }

            return duongPhoBOCollection;
        }
        #endregion Business Method
        //==========================================================================
    }
}
